package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.LoginUser;
import com.example.domain.entity.User;
import com.example.domain.entity.dto.UserInfoDto;
import com.example.exception.FloraException;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.example.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2022-11-21 20:20:13
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserInfoDto getUserInfoById(Long id) {
        User user = this.baseMapper.selectById(id);
        UserInfoDto userInfoDto= BeanCopyUtils.copyBean(user,UserInfoDto.class);
        return userInfoDto;
    }

    /** 查看个人信息 */
    @Override
    public UserInfoDto getUserInfo() {
        //获取当前用户id
        //Long userId= SecurityUtils.getUserId();
        User user = ((LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
        user = this.baseMapper.selectById(user.getId());

        UserInfoDto userInfoDto= BeanCopyUtils.copyBean(user,UserInfoDto.class);

        return userInfoDto;
    }

    @Override
    public UserInfoDto getSeller(String name) {
        QueryWrapper<User> qw=new QueryWrapper<>();
        qw.eq("nick_name",name).eq("type",2);
        User user=baseMapper.selectOne(qw);
        UserInfoDto userInfoDto= BeanCopyUtils.copyBean(user,UserInfoDto.class);
        return userInfoDto;
    }

    @Override
    public UserInfoDto getSellerById(Long id) {
        User user=baseMapper.selectById(id);
        UserInfoDto userInfoDto= BeanCopyUtils.copyBean(user,UserInfoDto.class);
        return userInfoDto;
    }

    /** 注册用户（上传头像时要调用upload方法） */
    @Override
    public void registerUser(User user) throws FloraException {
        //先对数据进行判断
        /** 用户名、密码、昵称都 不为空 */
        if(!StringUtils.hasText(user.getUserName())||!StringUtils.hasText(user.getNickName())
                ||!StringUtils.hasText(user.getPassword())){
            throw new FloraException("用户名、密码、昵称都不能为空");
        }

        /** 且 不重复 */
        if(userNameExist(user.getUserName())){
            throw new FloraException("用户名重复，请换一个");
        }

        /** 密码加密，存入数据库 */
        String encodePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePassword);
        save(user);
    }

    /** 更新用户信息 */
    @Override
    public void updateUserInfo(User user) throws FloraException {
        if(!StringUtils.hasText(user.getUserName())||!StringUtils.hasText(user.getNickName())
                ||!StringUtils.hasText(user.getPassword())){
            throw new FloraException("密码、昵称都不能为空");
        }

        String encodePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePassword);

        this.baseMapper.updateById(user);
    }

    /** 更新用户信息 */
    @Override
    public void editUserInfo(User user) throws FloraException {
        if(!StringUtils.hasText(user.getUserName())||!StringUtils.hasText(user.getNickName())){
            throw new FloraException("昵称、用户名都不能为空");
        }
        LambdaQueryWrapper<User> qw=new LambdaQueryWrapper<>();
        qw.eq(User::getId,user.getId());
        this.baseMapper.update(user,qw);
    }

    /** 停用用户 */
    @Override
    public void blockUser(User user) {
        user.setStatus(1);
        this.baseMapper.updateById(user);
    }

    public boolean nickNameExist(String nickName) {
        LambdaQueryWrapper<User> qw=new LambdaQueryWrapper<>();
        qw.eq(User::getNickName,nickName);
        return count(qw)>0;
    }

    public boolean userNameExist(String userName) {
        LambdaQueryWrapper<User> qw=new LambdaQueryWrapper<>();
        qw.eq(User::getUserName,userName);

        return count(qw) > 0;
    }
}

